package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPicBeautyBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.apis.ApiManager;
import stark.common.base.IReqRetCallback;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class PicBeautyActivity extends BaseAc<ActivityPicBeautyBinding> {

    public static String imgPath;
    private boolean hasOpenSpecialEffect = false;
    private Bitmap mSpecialEffectBmp;


    @Override
    protected int onCreate() {
        return R.layout.activity_pic_beauty;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicBeautyBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rlOrigin.setOnClickListener(this);
        mDataBinding.rlBeauty.setOnClickListener(this);
        mDataBinding.ivPicBeautySave.setOnClickListener(this);
        mDataBinding.ivImgCompare.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!hasOpenSpecialEffect) return true;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Glide.with(mContext).load(imgPath).into(mDataBinding.ivPicBeautyImg);
                        break;
                    case MotionEvent.ACTION_UP:
                        mDataBinding.ivPicBeautyImg.setImageBitmap(mSpecialEffectBmp);
                        break;
                }
                return true;
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlOrigin://原图
                clearSelection();
                mDataBinding.ivOriginalImgSel.setVisibility(View.VISIBLE);
                mDataBinding.ivOriginal.setVisibility(View.VISIBLE);
                mDataBinding.tvOriginal.setVisibility(View.GONE);
                Glide.with(mContext).load(imgPath).into(mDataBinding.ivPicBeautyImg);
                break;
            case R.id.rlBeauty://精修
                clearSelection();
                mDataBinding.ivBeautyImgSel.setVisibility(View.VISIBLE);
                mDataBinding.ivBeauty.setVisibility(View.VISIBLE);
                mDataBinding.tvBeauty.setVisibility(View.GONE);
                ApiManager.visionAiApi().facePretty(this, UriUtil.path2Uri(mContext,imgPath), new IReqRetCallback<Bitmap>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                        mDataBinding.ivPicBeautyImg.setImageBitmap(result);
                        mSpecialEffectBmp = result;
                        hasOpenSpecialEffect = true;
                    }
                });
                break;
            case R.id.ivPicBeautySave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(imgPath).into(mDataBinding.ivPicBeautyImg);
    }

    private void clearSelection() {
        mDataBinding.ivOriginalImgSel.setVisibility(View.GONE);
        mDataBinding.ivOriginal.setVisibility(View.GONE);
        mDataBinding.tvOriginal.setVisibility(View.VISIBLE);
        mDataBinding.ivBeautyImgSel.setVisibility(View.GONE);
        mDataBinding.ivBeauty.setVisibility(View.GONE);
        mDataBinding.tvBeauty.setVisibility(View.VISIBLE);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.ivPicBeautyImg);
                        //保存到相册
                        File file = ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext(file.getPath());
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        PicPreviewActivity.imgPath=path;
                        startActivity(PicPreviewActivity.class);
                    }
                });
            }
        }, 1000);
    }

}